<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高性能分布式限流系统设计指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2px 8px;
            border-radius: 4px;
            display: inline-block;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .algorithm-card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            position: relative;
            overflow: hidden;
        }
        
        .algorithm-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        }
        
        .tech-badge {
            display: inline-block;
            padding: 4px 12px;
            background: #f3f4f6;
            border-radius: 20px;
            font-size: 14px;
            margin: 4px;
            transition: all 0.3s ease;
        }
        
        .tech-badge:hover {
            background: #667eea;
            color: white;
            transform: scale(1.05);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    <i class="fas fa-shield-alt mr-4"></i>高性能分布式限流系统
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    构建稳定、可靠、高效的流量控制解决方案
                </p>
                <div class="flex flex-wrap justify-center gap-4">
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg px-6 py-3">
                        <i class="fas fa-tachometer-alt mr-2"></i>高并发支持
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg px-6 py-3">
                        <i class="fas fa-network-wired mr-2"></i>分布式架构
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg px-6 py-3">
                        <i class="fas fa-cogs mr-2"></i>动态配置
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>现代分布式系统中，流量控制是保障系统稳定性和高可用性的关键技术之一。随着用户规模的增长和业务需求的复杂化，系统往往需要面对突发流量或恶意攻击等场景，这就对限流机制提出了更高的要求。一套高性能的分布式限流系统，不仅能够精准控制流量，防止系统过载，还需要在高并发场景下具备低延迟、高吞吐的能力，同时满足多样化的业务需求。
                </p>
                <p class="text-lg leading-relaxed text-gray-700 mt-6">
                    从需求出发，我们需要明确限流的目标，例如按用户、接口或IP维度限制访问频率，确保系统资源的公平使用。其次，选择合适的限流算法至关重要，例如固定窗口计数、滑动窗口、令牌桶或漏桶算法，它们各有优缺点，需要结合业务场景选择合适的方案。同时，分布式场景中的限流不同于单机限流，需要应对多节点协同的问题，这就涉及到分布式存储和一致性方案的设计，比如借助 Redis 或 ZooKeeper 实现全局限流。
                </p>
            </div>
        </div>
    </section>

    <!-- System Architecture Diagram -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-3xl font-bold text-center mb-12">系统架构概览</h2>
                <div class="mermaid">
                    graph TB
                        A[客户端请求] --> B[API网关层]
                        B --> C{限流判断}
                        C -->|通过| D[服务层]
                        C -->|拒绝| E[返回限流响应]
                        D --> F[业务处理]
                        B --> G[配置中心]
                        B --> H[Redis集群]
                        G --> I[限流规则]
                        H --> J[计数器存储]
                        D --> K[监控系统]
                        K --> L[Prometheus]
                        K --> M[Grafana]
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style B fill:#bbf,stroke:#333,stroke-width:2px
                        style C fill:#fbf,stroke:#333,stroke-width:2px
                        style H fill:#bfb,stroke:#333,stroke-width:2px
                </div>
            </div>
        </div>
    </section>

    <!-- Requirements Analysis -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-4xl font-bold mb-12 section-title">需求分析</h2>
                <p class="text-lg text-gray-700 mb-8">
                    设计一个高性能的分布式限流系统，需求分析是关键的起点，它直接决定了系统的功能范围、实现方案以及性能要求。
                </p>
                
                <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">保护系统稳定性</h3>
                        <p class="text-gray-600">避免突发流量（如流量洪峰或恶意攻击）导致系统过载、服务不可用。</p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-balance-scale"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">优化资源利用</h3>
                        <p class="text-gray-600">通过限流保证公平分配资源，避免某些用户或接口占用过多系统资源。</p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-users"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">提升用户体验</h3>
                        <p class="text-gray-600">在限流情况下，提供平滑的降级策略，防止用户体验断崖式下滑。</p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-rocket"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">高并发支持</h3>
                        <p class="text-gray-600">在高流量场景下，限流系统需要能够承载数万甚至数十万的并发请求。</p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">低延迟响应</h3>
                        <p class="text-gray-600">限流判断需要快速完成，避免成为系统瓶颈。</p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-lg shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-expand-arrows-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">扩展性强</h3>
                        <p class="text-gray-600">系统需要支持动态扩容，适应流量变化。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Algorithm Selection -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-4xl font-bold mb-12 section-title">限流算法选择</h2>
                <p class="text-lg text-gray-700 mb-8">
                    限流算法是分布式限流系统的核心，它直接决定了限流的精准性、性能以及适用场景。选择合适的限流算法需要根据业务需求、系统负载特性和性能指标综合考虑。
                </p>
                
                <div class="space-y-6">
                    <div class="algorithm-card card-hover">
                        <h3 class="text-2xl font-bold mb-4">
                            <i class="fas fa-window-maximize mr-3 text-purple-600"></i>固定时间窗口算法
                        </h3>
                        <div class="grid md:grid-cols-2 gap-6">
                            <div>
                                <h4 class="font-semibold text-gray-700 mb-2">原理</h4>
                                <p class="text-gray-600">将时间划分为固定的窗口（如1秒、1分钟），统计窗口内的请求数。如果超过限流阈值，直接拒绝超出的请求。</p>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-700 mb-2">适用场景</h4>
                                <p class="text-gray-600">接口调用频率控制，如每秒允许最多100次调用。实现简单，适用于大多数限流场景。</p>
                            </div>
                        </div>
                        <div class="mt-4">
                            <span class="tech-badge"><i class="fas fa-check-circle mr-1"></i>简单易实现</span>
                            <span class="tech-badge"><i class="fas fa-memory mr-1"></i>内存占用小</span>
                            <span class="tech-badge"><i class="fas fa-exclamation-triangle mr-1"></i>存在突发流量